#ifndef PNIO_MEM_H
#define PNIO_MEM_H

#include <stdint.h>
#include <sys/sem.h>

#define PNDEVKEY 204
#define PNSEMKEY 5001

// plc -> robot
#define PN_DI_NUM 64
#define PN_AI_INT_NUM 32
#define PN_AI_FLOAT_NUM 32

// robot - >plc
#define PN_DO_NUM 64
#define PN_AO_INT_NUM 32
#define PN_AO_FLOAT_NUM 32

#define PN_JOINT_INT_NUM 6
#define PN_JOINT_FLOAT_INT_NUM 48
#define PN_JOINT_UINT8_NUM 4

#define PN_TCP_FLOAT_NUM 30
#define PN_BASE_FLOAT_NUM 6

#define PN_ROBOT_OUT_INT_NUM 2
#define PN_ROBOT_OUT_FLOAT_NUM 3
#define PN_ROBOT_OUT_UINT8_NUM 4

#define PN_SAFETY_OUT_INT_NUM 2
#define PN_SAFETY_OUT_UINT8_NUM 4
// srci data
#define PN_SRCI_OUT_NUM 256  ///< PLC->RC
#define PN_SRCI_IN_NUM 256   ///< RC->PLC

#define PN_ADAPTER_NAME_NUM 32

enum ProfinetState
{
    PfnStateDisable = 0,
    PfnStateUnsupportX64 = 1,
    PfnStateInvalidCfgFile = 2,
    PfnStateProtocalInitFailed = 3,
    PfnStateExpectionAbort = 4,
    PfnStateMasterDisConnect = 100,
    PfnStateMasterConnect = 101
};

typedef struct PNDevData
{
    int16_t DI[PN_DI_NUM];
    int32_t AI_INT[PN_AI_INT_NUM];
    float AI_FLOAT[PN_AI_FLOAT_NUM];

    int16_t DO[PN_DO_NUM];
    int32_t AO_INT[PN_AO_INT_NUM];
    float AO_FLOAT[PN_AO_FLOAT_NUM];

    int32_t JOINT_INT[PN_JOINT_INT_NUM];
    float JOINT_FLOAT[PN_JOINT_FLOAT_INT_NUM];
    uint8_t JOINT_UINT8[PN_JOINT_UINT8_NUM];

    float TCP_FLOAT[PN_TCP_FLOAT_NUM];
    float BASE_FLOAT[PN_BASE_FLOAT_NUM];

    int32_t ROBOT_OUT_INT[PN_ROBOT_OUT_INT_NUM];
    float ROBOT_OUT_FLOAT[PN_ROBOT_OUT_FLOAT_NUM];
    uint8_t ROBOT_OUT_UINT8[PN_JOINT_UINT8_NUM];

    int32_t SAFETY_OUT_INT[PN_SAFETY_OUT_INT_NUM];
    uint8_t SAFETY_OUT_UINT8[PN_SAFETY_OUT_UINT8_NUM];
} __attribute__((packed)) PNDevData;

// 该类型需要在32位和64位的进程中进行内存共享所以统一按照紧凑模式对齐数据
typedef struct
{
    char enable;
    char srci_active;
} __attribute__((packed)) PNDevControl;

typedef struct
{
    int state;
    char adapter_name[PN_ADAPTER_NAME_NUM];
    char name[128];
    unsigned int ip, mac, version;
    char led_state;  //-1:disable  0:lightoff  1:lighton
    char srci_state;
} __attribute__((packed)) PNDevState;

typedef struct
{
    uint8_t srci_out[PN_SRCI_OUT_NUM];  ///< PLC->RC
    uint8_t srci_in[PN_SRCI_IN_NUM];    ///< RC->PLC
} __attribute__((packed)) PNDevSrciData;

//该类型需要在32位和64位的进程中进行内存共享所以统一按照紧凑模式对齐数据
typedef struct PNDevShm
{
    PNDevControl control;
    PNDevState state;
    PNDevData pnio;
    PNDevSrciData srci;
} __attribute__((packed)) PNDevShm;

#endif